<!--
 * @Date: 2024-12-23 14:40:09
 * @LastEditors: Lcx l1556461835@163.com
 * @LastEditTime: 2025-03-20 16:49:00
 * @FilePath: \simulation-fontend\pages\components\FceSim.vue
 * @Description: 加热炉模型
-->

<template>
  <div>
    <el-card header="加热炉仿真/PDI输入" style="margin-bottom: 10px">
      <el-form :inline="true" :model="tbProcalcSumDTO" label-position="top">
        <el-form-item label="钢种" prop="steelGrade">
          <el-select v-model="tbProcalcSumDTO.steelGrade" placeholder="钢种" @change="handleSteelChange">
            <el-option v-for="item in steelList" :key="item.id" :label="item.steelGrade" :value="item.steelGrade"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="板坯长度[mm]" prop="slabLen">
          <el-input v-model.number.trim="tbProcalcSumDTO.slabLen" placeholder="板坯长度" @input="(v)=>(tbProcalcSumDTO['slabLen'] = v.replace(/[^\d.]/g,''))"></el-input>
        </el-form-item>
        <el-form-item label="板坯厚度[mm]" prop="en_thk">
          <el-input v-model.number.trim="tbProcalcSumDTO.en_thk" placeholder="板坯厚度" @input="(v)=>(tbProcalcSumDTO['en_thk'] = v.replace(/[^\d.]/g,''))"></el-input>
        </el-form-item>
        <el-form-item label="在炉时间[min]" prop="mdl_in_time0">
          <el-input v-model.number.trim="tbProcalcSumDTO.mdl_in_time0" placeholder="在炉时间" @input="(v)=>(tbProcalcSumDTO['mdl_in_time0'] = v.replace(/[^\d.]/g,''))"></el-input>
        </el-form-item>
        <el-form-item label="成品宽度[mm]" prop="slabWid">
          <el-input v-model.number.trim="tbProcalcSumDTO.slabWid" placeholder="成品宽度" @change="handleWidChange" @input="(v)=>(tbProcalcSumDTO['slabWid'] = v.replace(/[^\d.]/g,''))"></el-input>
        </el-form-item>
        <el-form-item label="成品厚度[mm]" prop="slabThk">
          <el-input v-model.number.trim="tbProcalcSumDTO.slabThk" placeholder="成品厚度" @change="handleThkChange" @input="(v)=>(tbProcalcSumDTO['slabThk'] = v.replace(/[^\d.]/g,''))"></el-input>
        </el-form-item>
        <el-form-item label="入炉温度[℃]" prop="dischtrgtemp">
          <el-input v-model.number.trim="tbProcalcSumDTO.dischtrgtemp" placeholder="入炉温度" @input="(v)=>(tbProcalcSumDTO['dischtrgtemp'] = v.replace(/[^\d.]/g,''))"></el-input>
        </el-form-item>
        <el-form-item label="炉号" prop="versioncode">
          <el-select v-model="tbProcalcSumDTO.versioncode" placeholder="炉号">
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
            <el-option label="3" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="flag !== 1" label="目标温度[℃]" prop="mdl_ex_temp0">
          <el-input v-model.number.trim="tbProcalcSumDTO.mdl_ex_temp0" placeholder="目标温度"></el-input>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 化学元素 -->
    <el-card style="margin: 10px 0">
      <BasicForm ref="huaRef" :form-list="huaxueList" :form-info="elementDTO" inline ></BasicForm>
    </el-card>
    <el-card v-if="flag == 1" style="margin-bottom:10px" header="仿真结果（温度曲线和晶体尺寸曲线）">
      <hmi-echarts :x-axis="chart.xAxis" :y-axis="chart.yAxis" :series="chart.series" :grid="chart.grid" :tooltip="chart.tooltip"></hmi-echarts>
    </el-card>
    <!-- 仿真结果 -->
    <el-card v-if="flag == 1" header="仿真结果" style="margin-bottom:10px">
      <el-form :inline="true" :model="tbSetpointSumVO" label-position="left">
        <el-form-item label="出炉温度[℃]">
          <el-input v-model.number.trim="tbSetpointSumVO.exitTemp1" placeholder="出炉温度"></el-input>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card header="设定工艺">
      <BasicForm :form-list="gongyi" :form-info="tbProcalcSumDTO" inline label-pos="left" ></BasicForm>
      <el-form :model="tbProcalcSumDTO" inline>
        <el-form-item label="任务名称">
          <el-input v-model.trim="tbProcalcSumDTO.name" placeholder="任务名称"></el-input>
        </el-form-item>
        <el-form-item label="提交人">
          <el-input v-model.trim="tbProcalcSumDTO.subPerson" placeholder="提交人"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model.trim="tbProcalcSumDTO.note" :rows="5" type="textarea" placeholder="请输入备注"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button v-if="flag !== 1" type="primary" size="small" @click="submit">提交</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import BasicForm from "../components/BasicForm.vue";
import { FceParams, setGong } from "@/lib/commonVar.js";
import { post } from "@/lib/Util";
import HmiEcharts from "../../components/HmiEcharts.vue";
export default {
  components: { BasicForm, HmiEcharts },
  props: {
    formInfo: {
      type: Object,
      default: () => {}
    },
    flag: {
      type: Number,
      default: 0 // 0 新建, 1 查看, 2 编辑
    }
  },
  data() {
    return {
      tbProcalcSumDTO: {
        preheatTime: undefined,
        onestheatTime: undefined,
        twondheatTime: undefined,
        soakingTime: undefined,
        preheattemp: undefined,
        onestheattemp: undefined,
        twondheattemp: undefined,
        soakingtemp: undefined,
        subPerson: undefined,
        mdl_ex_temp0: undefined,
        exitTemp1: undefined
      },
      huaxueList: FceParams,
      gongyi: setGong,
      steelList: [],
      elementDTO: {}, // 保存化学元素
      tbSetpointSumVO: {
        exitTemp1: undefined
      },
      chart: {
        xAxis: [
          {
            type: "category",
            show: true,
            axisTick: {
              alignWithLabel: true
            },
            data: []
          }
        ],
        yAxis: [
          {
            name: "温度(℃)",
            type: "value"
          },
          {
            name: "晶体尺寸",
            nameLocation: "end",
            type: "value"
          }
        ],
        series: [
          {
            name: "温度",
            type: "line",
            data: []
          },
          {
            name: "尺寸",
            type: "line",
            data: [],
            yAxisIndex: 1
          }
        ],
        grid: [
          {
            bottom: 40
          }
        ],
        tooltip: {
          show: true,
          trigger: "item"
        }
      },
      tbCrystalstrFurVO: {
        valueTime: "",
        valueGrainSize: ""
      }
    };
  },
  mounted() {
    this.getSteelGrade();
    this.$refs.huaRef.resetForm();
    if (this.flag == 1) {
      this.tbProcalcSumDTO = this.formInfo.tbProcalcSumVO;
      this.elementDTO = this.formInfo.elementVO;
      this.tbSetpointSumVO = this.formInfo.tbSetpointSumVO;
      this.tbCrystalstrFurVO = this.formInfo.tbCrystalstrFurVO;
      this.getDrawLine();
    } else if (this.flag == 2) {
      this.tbProcalcSumDTO = this.formInfo.tbProcalcSumVO;
      this.elementDTO = this.formInfo.elementVO;
    }
  },
  methods: {
    // 绘制曲线
    getDrawLine() {
      let tempList = [];
      let valueGrainSize = [];
      let valueTime = [];
      if (this.tbSetpointSumVO && this.tbSetpointSumVO.mdltemp) {
        tempList = this.tbSetpointSumVO.mdltemp.split("&");
      }
      if (this.tbCrystalstrFurVO.valueGrainSize.length) {
        valueGrainSize = this.tbCrystalstrFurVO.valueGrainSize.split("&");
      }
      if (this.tbCrystalstrFurVO.valueTime.length) {
        valueTime = this.tbCrystalstrFurVO.valueTime.split("&");
      }
      const tempLineSeries = this.averageSample(tempList, valueTime);
      this.chart.xAxis[0].data = valueTime;
      this.chart.series[0].data = tempLineSeries;
      this.chart.series[1].data = valueGrainSize;
    },
    averageSample(largeArray, smallArray) {
      const largeLen = largeArray.length; // 54
      const smallLen = smallArray.length; // 13

      if (smallLen > largeLen) {
        return [];
      }

      // 计算步长，确保我们能平均地从largeArray中抽取smallLen个元素
      const step = (largeLen - 1) / (smallLen - 1);

      const selectedValues = [];
      for (let i = 0; i < smallLen; i++) {
        // 根据步长计算索引位置，使用Math.round来选择最接近的整数索引
        const index = Math.round(i * step);
        selectedValues.push(largeArray[index]);
      }

      // 现在selectedValues和smallArray有对应的值
      return selectedValues;
    },

    // 查询钢种
    async getSteelGrade() {
      let res = await post("/api/tbChemical/getSteelGrade", {});
      this.steelList = res.data;
    },
    handleSteelChange(val) {
      if (val) {
        let item = this.steelList.filter(item => item.steelGrade == val);
        post(`/api/tbChemical/byIdSelect?id=${item[0].id}`).then(res => {
          this.elementDTO = res.data;
        });
      } else {
        this.$refs.huaRef.resetForm();
      }
    },
    // 查看设定工艺
    handleWidChange(val) {
      if (val) {
        if (this.tbProcalcSumDTO.slabThk) {
          this.getSet();
        }
      }
    },
    handleThkChange(val) {
      if (val) {
        if (this.tbProcalcSumDTO.slabWid) {
          this.getSet();
        }
      }
    },
    async getSet() {
      let res = await post(
        `/api/tbSchData/selectCraft?matClass=1&slabThk=${
          this.tbProcalcSumDTO.slabThk
        }&slabWid=${this.tbProcalcSumDTO.slabWid}`
      );
      if (res.data) {
        this.tbProcalcSumDTO = Object.assign(this.tbProcalcSumDTO, res.data);
      }
    },
    // 提交事件
    submit() {
      this.tbProcalcSumDTO["calcType"] = 6; // 标记这是加热炉仿真
      post("/api/tbProcalcSum/heatingFurnaceSave", {
        tbProcalcSumDTO: this.tbProcalcSumDTO,
        elementDTO: this.elementDTO
      }).then(res => {
        if (res.code == 200) {
          this.$message({
            message: "提交成功",
            type: "success"
          });
        } else {
          this.$message.error(res.message);
        }
        this.$emit("close");
      });
    }
  }
};
</script>

<style>
</style>
